package com.baymx.plugin.demo

import android.R
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.shapes.RectShape
import android.graphics.drawable.shapes.Shape
import android.os.Build
import android.util.Log
import android.util.StateSet
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.widget.*
import androidx.core.app.NotificationCompat
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexLine
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayout
import com.google.android.flexbox.FlexboxLayout.*
import com.google.android.flexbox.JustifyContent
import java.util.concurrent.atomic.AtomicInteger

fun newTxtColor(): ColorStateList {
    return ColorStateList(
        arrayOf(
            intArrayOf(R.attr.state_focused), intArrayOf(R.attr.state_selected),
            StateSet.WILD_CARD
        ), intArrayOf(Color.BLACK, Color.RED, Color.WHITE)
    )
}

fun ViewGroup.addButton(
    text: String,
    block: TextView.() -> Unit = {},
    onclick: Button.() -> Unit = {}
): Button {
    val ctx = this.context
    val btn = Button(ctx).apply {
        this.text = text
        setTextColor(newTxtColor())
        Log.d("addButton", "textColor:${textColors} ");
        setOnClickListener { onclick() }
        block()

    }
    this.addView(btn)
    return btn

}

fun ViewGroup.newScrollContainer(init: LinearLayout.() -> Unit = {}) =
    ScrollView(this.context).apply {
        clipToPadding = false
        clipChildren = false
        id = View.generateViewId()
        val ll = LinearLayout(this.context).apply {
            orientation = LinearLayout.VERTICAL
            setPadding(0, 30, 0, 30)
            clipToPadding = false
            clipChildren = false
            init(this)
        }
        this.addView(ll, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
    }

fun  ViewGroup.newSceneFlexContainer(
    init: FlexboxLayout.() -> Unit = {}
) = this.newFlexContainer(false ,false ,init)
fun ViewGroup.newFlexContainer(
    scrollable: Boolean = false,
    attachParent: Boolean = true,
    init: FlexboxLayout.() -> Unit = {}
): View {
    val flex = FlexboxLayout(this.context).apply {
        setPadding(0, 30, 0, 0)
        clipToPadding = false
        clipChildren = false
        flexWrap = FlexWrap.WRAP
        justifyContent = JustifyContent.FLEX_START
        flexDirection = FlexDirection.ROW
        setShowDivider(FlexboxLayout.SHOW_DIVIDER_MIDDLE)
        setDividerDrawable(ShapeDrawable(RectShape().apply {
            resize(10f, 10f)
        }))
        init(this)
    }
    var returnView: View = flex
    if (scrollable) {
        returnView = ScrollView(this.context).apply {
            clipToPadding = false
            clipChildren = false
            id = View.generateViewId()
            this@newFlexContainer.addView(
                this,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        }
        returnView.addView(flex, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
    }
    if (attachParent) {
        this.addView(returnView)
    }
    return returnView
}


fun ViewGroup.addText(txt: String = "", hint: String = ""): TextView {
    val ctx = this.context
    return TextView(ctx).also {
        this.addView(it)
        it.setText(txt)
        it.hint = hint

    }
}

fun <T:View> ViewGroup.addDFView(
    width: Int = -2,
    height: Int = -2,
    v: T? = null,
    apply: T.() -> Unit = {}
) =
    addView((v?.apply(apply) ?: View(context)), MarginLayoutParams(width, height))


const val NID = "test_scene"
private val NotificationCompatIdGetter = AtomicInteger(1)
private fun Context.createNChannel(): Boolean {
    if (Build.VERSION.SDK_INT >= 26) {
        //Android O上对Notification进行了修改，如果设置的targetSDKVersion>=26建议使用此种方式创建通知栏
        val notificationManager =
            this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        if (notificationManager.getNotificationChannel(NID) == null) {//没有创建
            // 用户可以看到的通知渠道的名字.
            val name: CharSequence = "厂房在线通知"
            // 用户可以看到的通知渠道的描述
            val description = "厂房在线通知"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val notificationChannel = NotificationChannel(NID, name, importance)
            notificationChannel.setDescription(description);
            notificationChannel.enableLights(true) //是否在桌面icon右上角展示小圆点
            notificationChannel.lightColor = Color.BLUE //小圆点颜色
            notificationChannel.setShowBadge(true) //是否在久按桌面图标时显示此渠道的通知
            notificationManager.createNotificationChannel(notificationChannel)
            Log.d("createNChannel", "createNChannel $NID ok!")
        }
    }
    return true
}


@SuppressLint("NewApi")
fun Context.buildNotification(f: NotificationCompat.Builder.() -> Unit): Notification {
    createNChannel()
    val builder = NotificationCompat.Builder(this, NID)
    f(builder)
    return builder.build()
}

fun createNotificationId(): Int {
    return NotificationCompatIdGetter.getAndIncrement()
}